<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            收货地址
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>
    <div id="list">
    </div>
    <div style="height: 2.8rem;"></div>
    <div class="commission-btn" tapmode onclick="addressadd()">新建地址</div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="address-item">
        <div class="address-item-t">
            <p>{{=value.name}}<span>{{=value.tel}}</span></p>
            <p>{{=value.city}}{{=value.region}}{{=value.address}}</p>
        </div>
        <div class="address-item-d">
            <div class="address-item-d-l">
                <label><input class="aui-radio" type="radio" name="demo1" onclick="return setDefaultAddress({{=value.add_id}});"
                    {{? add_id_default == value.add_id}}
                    checked
                    {{?}}
                    >设置默认</label>
                </div>
                <div class="address-item-d-r">
                    <div tapmode onclick="addressedit({{=value.add_id}});"><i class="fa fa-edit"></i>编辑</div>
                    <div tapmode onclick="addressDel({{=value.add_id}});"><i class="fa fa-trash-o"></i>删除</div>
                </div>
            </div>
        </div>
        {{~}}
    </script>
    <script type="text/javascript">

        apiready = function(){
        reloadListener(); // 添加监听事件 (页面刷新)
        getDefaultAddress(); // 获取默认收货地址
        // loadData();
    }
    
    function loadData(){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Address/index',
            method: 'get',
            data: {
                values: {
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                if (ret.result) {

                    templateFun(ret.result);
                }

            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data){
        if (data[0]) {
          var template = $api.byId('template');
          var dotFn = doT.template(template.innerHTML);
          var html = dotFn(data);
          var list = $api.byId('list');
          $api.html(list, html);
      }else{

          var list = $api.byId('list');
          var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:1rem;">暂无内容</h2>';
          list.innerHTML = html;
      }
  }

    // 编辑地址
    function addressedit(add_id){
        var delay = 0;
        if (api.systemType != 'ios') {
            delay = 300;
        }
        api.openWin({
            name: 'addressedit',
            url: './address_edit.html',
            bounces: false,
            delay: delay,
            slidBackEnabled: true,
            vScrollBarEnabled: false,
            progress: {
                type: "page"
            },
            pageParam:{
                id: add_id
            }
        });
    }

    // 新增地址
    function addressadd(add_id){

        var delay = 0;
        if (api.systemType != 'ios') {
            delay = 300;
        }
        api.openWin({
            name: 'addressadd',
            url: './address_add.html',
            bounces: false,
            delay: delay,
            slidBackEnabled: true,
            vScrollBarEnabled: false,
            progress: {
                type: "page"
            },

        });
    }

    // 删除地址
    function addressDel(add_id){

        api.confirm({
            title: '提示!',
            msg: '确认删除?',
            buttons: ['确定', '取消']
        }, function(ret, err){
            if( ret ){
             if (ret.buttonIndex == 1) {
                var userid = $api.getStorage('userid');
                var authtoken = $api.getStorage('authtoken');
                api.ajax({
                    url: webAddress + '/a/order/Address/del',
                    method: 'get',
                    data: {
                        values: {
                            id: add_id,
                        },
                    },
                    headers: {
                        TOKEN: token,
                        authuid: userid,
                        authtoken: authtoken,
                        'Content-Type': 'application/json',
                    },
                },function(ret, err){
                    if (ret) {
                        // alert( JSON.stringify( ret ) );
                        api.toast({
                            msg: ret.message,
                            duration: 2000,
                            location: 'bottom',
                            global: 'true',
                        });
                        loadData();
                    } else {
                        // alert( JSON.stringify( err ) );
                        api.toast({
                            msg: err.body.message,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return false;
                    }
                });
            }
        }else{
         alert( JSON.stringify( err ) );
     }
 });

    }

   //  // 设置默认收货地址
   //  function setDefaultAddress(add_id){
   //      api.confirm({
   //          title: '提示!',
   //          msg: '确认设置为默认?',
   //          buttons: ['确定', '取消']
   //      }, function(ret, err){
   //          if( ret ){
   //             if (ret.buttonIndex == 1) {
   //              var userid = $api.getStorage('userid');
   //              var authtoken = $api.getStorage('authtoken');
   //              api.ajax({
   //                  url: webAddress + '/a/order/Address/setting',
   //                  method: 'get',
   //                  data: {
   //                      values: {
   //                          id: add_id,
   //                      },
   //                  },
   //                  headers: {
   //                      TOKEN: token,
   //                      authuid: userid,
   //                      authtoken: authtoken,
   //                      'Content-Type': 'application/json',
   //                  },
   //              },function(ret, err){
   //                  if (ret) {
   //                      alert( JSON.stringify( ret ) );
   //                      api.toast({
   //                          msg: ret.message,
   //                          duration: 2000,
   //                          location: 'bottom',
   //                          global: 'true',
   //                      });
   //                      // loadData();
   //                      getDefaultAddress();
   //                  } else {
   //                      alert( JSON.stringify( err ) );
   //                      api.toast({
   //                          msg: err.body.message,
   //                          duration: 2000,
   //                          location: 'bottom'
   //                      });
   //                      return false;
   //                  }
   //              });
   //          }else{
   //              return false;
   //          }
   //      }else{
   //         alert( JSON.stringify( err ) );
   //     }
   // });
   //      return false;
   //  }

    // 设置默认收货地址
    function setDefaultAddress(add_id){

        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Address/setting',
            method: 'get',
            data: {
                values: {
                    id: add_id,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.toast({
                    msg: ret.message,
                    duration: 2000,
                    location: 'bottom',
                    global: 'true',
                });
                // loadData();
                getDefaultAddress();
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 获取默认收货地址
    function getDefaultAddress(){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Address/default',
            method: 'get',
            data: {
                values: {
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                add_id_default = ret.result.add_id;
                loadData();                
            } else {
                alert(123)
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 监听事件 (页面刷新)
    function reloadListener(){
        api.addEventListener({
            name: 'reload',
        }, function(ret, err){
         getDefaultAddress();
     });
    }

    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>